package day04;
import java.util.Arrays;
import java.util.Objects;
import java.util.Scanner;

public class ShopSystem {
    static String[] names = {"iPhone15", "华为 Mate 90 pro", "小米XXL", "vivo NEX", "oppo "};
    static double[] price = {8999, 5399, 2399, 4399, 3999};
    static int[] numbers = {50, 20, 80, 120, 90};
    static String[] userName = {"Miyako"};
    static String[] userPassword = {"Ss147852"};
    static String[] adminName = {"admin"};
    static String[] adminPassword = {"Ss147852"};
    static int st = 1;
    static Scanner sc6 = new Scanner(System.in);
    static int state = 0;//状态函数
    static int logstate = 0;//登录状态0未登录 1管理员 2普通用户

    public static void main(String[] args) {
        if (logstate == 0) {
            Scanner sc = new Scanner(System.in);
            Login();//进入登录页面
            logstate = 1;//保持登录状态
        }
        System.out.println("按任意键进入菜单");
        int i = sc6.nextInt();
        System.out.print((String.format("%-3s", "| ————主菜单———— |\n| 1.商品信息展示 |\n| 2.商品信息录入 |\n| 3.商品信息查询 |\n| 4.商品信息删除|\n| 5.退出登录 |\n 选择：>  ")));
        i = sc6.nextInt();
        switch (i) {
            case 1:
                ShopShow();
                break;
            case 2:
                InputShop();
                break;
            case 3:
                SelectProduct();
                break;
            case 4:
                DeletePrice();
                break;
            case 5:
                logstate = 0;//重置登录状态
                main(names);

        }
    }

    public static void ShopShow() {
        System.out.println((String.format("%-30s%-30s%-30s", "商品名", "单价", "库存")));
        for (int i = 0; i < names.length; i++) {
            if (names[i].charAt(0) > 20000) {//汉字会多占一个字符，但是format方法识别不到设定30实际会占用31字符
                System.out.println((String.format("%-29s%-30s%-30s", names[i], price[i], numbers[i])));
            } else
                System.out.println((String.format("%-30s%-30s%-30s", names[i], price[i], numbers[i])));
        }
        System.out.println("1.根据价格排序\n2.根据库存排序\n3.退出\n选择：>");
        int i = new Scanner(System.in).nextInt();
        if (i == 3)
            main(names);
        else if (i == 1) {
            PriceRank();
        } else if (i == 2) {
            NumbersRank();
        }

    }

    public static void InputShop() {
        Scanner sc1 = new Scanner(System.in);
        System.out.println((String.format("%-30s", "输入-1录入新数据\n输入1-" + names.length + "选择数据进行修改")));
        for (; ; ) {
            int s = sc1.nextInt();
            while (s == -1) {//添加新数据
                names = Arrays.copyOf(names, names.length + 1);
                price = Arrays.copyOf(price, price.length + 1);//数组扩容
                numbers = Arrays.copyOf(numbers, numbers.length + 1);
                int i = names.length;
                System.out.println((String.format("%-30s", "商品名:")));
                names[i - 1] = sc1.nextLine();//拿来接收上个输出结束后的\n否则会跳过这次输入
                names[i - 1] = sc1.nextLine();
                System.out.println((String.format("%-30s", "价格:")));
                price[i - 1] = sc1.nextDouble();
                System.out.println((String.format("%-30s", "库存:")));
                numbers[i - 1] = sc1.nextInt();
                System.out.println("录入完毕是否继续？\n 1.是\n 2.否(退出)");
                i = sc1.nextInt();
                if (i == 2) {
                    main(names);
                }
            }

            System.out.println((String.format("%-30s", "开始修改第" + s + "条数据 " + "商品名：" + names[s - 1], "商品价格：" + price[s - 1], "商品库存：" + numbers[s - 1])));
            System.out.print((String.format("%-50s", "| 您要修改什么:     |\n| 1.商品名 |\n| 2.商品价格 |\n| 3.商品库存 |\n| 4.所有        |\n| 5.取消修改      |\n选择：>  ")));
            int i = sc1.nextInt();
            switch (i) {//修改第i项数据
                case 1:
                    System.out.println((String.format("%-30s", "商品名:")));
                    names[s - 1] = sc1.nextLine();
                    break;
                case 2:
                    System.out.println((String.format("%-30s", "价格:")));
                    price[s - 1] = sc1.nextDouble();
                    break;
                case 3:
                    System.out.println((String.format("%-30s", "库存:")));
                    numbers[s - 1] = sc1.nextInt();
                    break;
                case 4:
                    System.out.println((String.format("%-30s", "商品名:")));
                    names[s - 1] = sc1.nextLine();//拿来接收上个输出结束后的\n，否则会跳过这次输入
                    names[s - 1] = sc1.nextLine();
                    System.out.println((String.format("%-30s", "价格:")));
                    price[s - 1] = sc1.nextDouble();
                    System.out.println((String.format("%-30s", "价格:")));
                    numbers[s - 1] = sc1.nextInt();
                    break;
                default:
                    break;

            }
            System.out.println("是否继续录入？\n 1.是 \n 2.否(退出) \n 输入：>");
            i = sc1.nextInt();
            if (i == 2) {
                main(names);
            }
        }

    }

    public static void SelectProduct() {//分类+模糊查询
        Scanner sc2 = new Scanner(System.in);
        for (; ; ) {
            System.out.println("输入您要使用的索引\n 1.商品名 \n 2.商品价格 \n 3.商品库存");
            System.out.print("选择：>");
            int a = sc2.nextInt();
            switch (a) {
                case 1:
                    SelectName();
                    break;
                case 2:
                    SelectPrice();
                    break;
                case 3:
                    SelectNumber();
                    break;
            }
            System.out.println("是否继续查询？\n 1.是 \n 2.否(退出) ");
            System.out.print("选择：>");
            int i = sc2.nextInt();
            if (i == 2) {
                main(names);
            }

        }
    }

    public static void SelectName() {//商品名为索引
        Scanner sc3 = new Scanner(System.in);
        System.out.println("输入你要查找的商品名：");
        System.out.print("输入：>");
        String selectName = sc3.nextLine();
        for (int i = 0; i < names.length; i++) {
            if (selectName.equals(names[i]) || names[i].contains(selectName)) {//前面为精准查找，后面为模糊查询
                System.out.println("商品名:" + names[i] + "商品价格:" + price[i] + "商品库存:" + numbers[i]);
            }
        }
    }

    public static void SelectPrice() {
        Scanner sc3 = new Scanner(System.in);
        System.out.println("输入你要查找的商品最高价格：");
        System.out.print("输入：>");
        double selectPrice = sc3.nextDouble();
        for (int i = 0; i < price.length; i++) {
            if (selectPrice >= price[i]) {
                System.out.println("商品名:" + names[i] + "商品价格:" + price[i] + "商品库存:" + numbers[i]);
            }
        }
    }

    public static void SelectNumber() {
        Scanner sc4 = new Scanner(System.in);
        System.out.println("输入你要查找的最少商品库存量：");
        System.out.print("输入：>");
        int selectNumber = sc4.nextInt();
        for (int i = 0; i < numbers.length; i++) {
            if (selectNumber <= numbers[i]) {
                System.out.println("商品名:" + names[i] + "商品价格:" + price[i] + "商品库存:" + numbers[i]);
            }
        }
    }

    public static void NumbersRank() {
        Scanner sc0 = new Scanner(System.in);
        for (; ; ) {
            System.out.println("选择排序方式：\n1.升序\n2.降序 ");
            System.out.print("选择：>");
            int r = sc0.nextInt();
            for (; ; ) {
                if (r == 1)
                    for (int i = 0; i < numbers.length; i++) {//库存升序
                        for (int k = i + 1; k < numbers.length; k++) {
                            if (numbers[k] > numbers[i]) {
                                price[i] += price[k];
                                names[i] += names[k];
                                numbers[i] += numbers[k];

                                price[k] = price[i] - price[k];
                                names[k] = names[i].substring(0, names[i].length() - names[k].length());
                                numbers[k] = numbers[i] - numbers[k];

                                price[i] -= price[k];
                                names[i] = names[i].substring(names[k].length());
                                numbers[i] -= numbers[k];
                            }

                        }
                    }
                else
                    for (int i = numbers.length - 1; i > 0; i--) {//库存降序
                        for (int k = i - 1; k >= 0; k--) {
                            if (numbers[k] > numbers[i]) {
                                price[i] += price[k];
                                names[i] += names[k];
                                numbers[i] += numbers[k];

                                price[k] = price[i] - price[k];
                                names[k] = names[i].substring(0, names[i].length() - names[k].length());
                                numbers[k] = numbers[i] - numbers[k];

                                price[i] -= price[k];
                                names[i] = names[i].substring(names[k].length());
                                numbers[i] -= numbers[k];
                            }

                        }
                    }
                System.out.println("排序成功！\n\n");
                for (int i = 0; i < names.length; i++) {
                    if (names[i].charAt(0) > 20000) {//汉字会多占一个字符，但是format方法识别不到,如果设定30实际会占用31字符
                        System.out.println((String.format("%-5d%-29s%-30s%-30s", (i + 1), names[i], price[i], numbers[i])));
                    } else
                        System.out.println((String.format("%-5d%-30s%-30s%-30s", (i + 1), names[i], price[i], numbers[i])));
                }
                System.out.println("是否继续排序？\n 1.是 \n 2.否(退出) ");
                System.out.print("选择：>");
                int i = sc0.nextInt();
                if (i == 2) {
                    main(names);
                }
            }
        }
    }

    public static void PriceRank() {
        Scanner sc0 = new Scanner(System.in);
        for (; ; ) {
            System.out.println("选择排序方式：\n1.升序\n2.降序");
            System.out.print("选择：>");
            int r = sc0.nextInt();
            if (r == 1)
                for (int i = 0; i < price.length; i++) {//价格升序
                    for (int k = i + 1; k < price.length; k++) {
                        if (price[k] > price[i]) {
                            price[i] += price[k];
                            names[i] += names[k];
                            numbers[i] += numbers[k];

                            price[k] = price[i] - price[k];
                            names[k] = names[i].substring(0, names[i].length() - names[k].length());
                            numbers[k] = numbers[i] - numbers[k];

                            price[i] -= price[k];
                            names[i] = names[i].substring(names[k].length());
                            numbers[i] -= numbers[k];
                        }

                    }
                }
            else
                for (int i = price.length - 1; i > 0; i--) {//价格降序
                    for (int k = i - 1; k >= 0; k--) {
                        if (price[k] > price[i]) {
                            price[i] += price[k];
                            names[i] += names[k];
                            numbers[i] += numbers[k];

                            price[k] = price[i] - price[k];
                            names[k] = names[i].substring(0, names[i].length() - names[k].length());
                            numbers[k] = numbers[i] - numbers[k];

                            price[i] -= price[k];
                            names[i] = names[i].substring(names[k].length());
                            numbers[i] -= numbers[k];
                        }

                    }
                }
            System.out.println("排序成功！\n\n");
            for (int i = 0; i < names.length; i++) {
                if (names[i].charAt(0) > 20000) {//汉字会多占一个字符，但是format方法识别不到,如果设定30实际会占用31字符
                    System.out.println((String.format("%-5d%-29s%-30s%-30s", (i + 1), names[i], price[i], numbers[i])));
                } else
                    System.out.println((String.format("%-5d%-30s%-30s%-30s", (i + 1), names[i], price[i], numbers[i])));
            }
            System.out.println("是否继续排序？\n 1.是 \n 2.否(退出)");
            System.out.print("选择：>");
            int i = sc0.nextInt();
            if (i == 2) {
                main(names);
            }
        }
    }

    public static void DeletePrice() {
        Scanner sc4 = new Scanner(System.in);
        for (; ; ) {
            for (int i = 0; i < names.length; i++) {
                if (names[i].charAt(0) > 20000) {//汉字会多占一个字符，但是format方法识别不到,如果设定30实际会占用31字符
                    System.out.println((String.format("%-5d%-29s%-30s%-30s", (i + 1), names[i], price[i], numbers[i])));
                } else
                    System.out.println((String.format("%-5d%-30s%-30s%-30s", (i + 1), names[i], price[i], numbers[i])));
            }
            System.out.println("选择要删除的数据编号");
            System.out.print("选择：>");
            int del = sc4.nextInt();
            if (del != names.length + 1)//如果为最后一条数据只需要删除即可
                for (int i = del - 1; i < names.length - 1; i++) {//通过覆盖数据实现删除数据的同时，补全空白
                    names[i] = names[i + 1];
                    price[i] = price[i + 1];
                    numbers[i] = numbers[i + 1];
                }
            names = Arrays.copyOf(names, names.length - 1);
            price = Arrays.copyOf(price, price.length - 1);
            numbers = Arrays.copyOf(numbers, numbers.length - 1);
            System.out.println("删除成功！\n\n\n\n");
            for (int i = 0; i < names.length; i++) {
                if (names[i].charAt(0) > 20000) {//汉字会多占一个字符，但是format方法识别不到,如果设定30实际会占用31字符
                    System.out.println((String.format("%-5d%-29s%-30s%-30s", (i + 1), names[i], price[i], numbers[i])));
                } else
                    System.out.println((String.format("%-5d%-30s%-30s%-30s", (i + 1), names[i], price[i], numbers[i])));
            }
            System.out.println("还需要继续删除吗？\n 1.是 \n 2.否");
            System.out.print("选择：>");
            del = sc4.nextInt();
            if (del == 2) {
                main(names);
            }

        }
    }

    public static void Login() {
        Scanner sc5 = new Scanner(System.in);
        for (; ; ) {
            System.out.print((String.format("%-3s", "——————欢迎光临商店系统——————\n| 1.用户登录 \n| 2.管理员登录 \n| 3.注册 \n| 4.忘记密码 \n  \t")));
            System.out.print("选择：> ");
            int sta = sc5.nextInt();
            String user, password;
            if (sta == 1) {
                UserLogin();
                break;//登录成功直接进主菜单
            } else if (sta == 2) {
                AdminLogin();
                break;//登录成功直接进主菜单
            } else if (sta == 3) {
                Register();//注册后去登录
            } else if (sta == 4) {
                RestPassword();//重置后去登录
            }
        }
    }

    public static void UserLogin() {
        for (int z = 0, count = 0; z == 0; ) {
            System.out.println("——————用户登录——————");
            System.out.print("输入你的账号：>");
            String user = sc6.next();
            System.out.print("输入你的密码：>");
            String password = sc6.next();
            System.out.println();
            for (int i = 0; i < userName.length; i++) {
                if (Objects.equals(user, userName[i]) && Objects.equals(password, userPassword[i])) {
                    System.out.println("密码正确，登录成功");
                    z = 1;
                }
            }
            if (z == 0) {
                count++;
                if (count == 5) {//登录次数限制
                    System.out.println("尝试次数太多，程序已终止");
                    System.exit(1);//退出程序
                }
                System.out.println("登录失败，是否继续尝试？");
                System.out.println("|1.是 \n|2.否（退出）");
                System.out.print("选择：>");
                int h = sc6.nextInt();
                if (h == 2)
                    main(names);
                else if (h == 1)
                    System.out.println("再登录失败" + (5 - count) + "次程序将终止");
            }
        }

    }


    public static void AdminLogin() {
        for (int z = 0, count = 0; z == 0; ) {
            System.out.println("————管理员登录————");
            System.out.print("输入你的账号：>");
            String user = sc6.next();
            System.out.println();
            System.out.print("输入你的密码：>");
            String password = sc6.next();
            for (int i = 0; i < adminName.length; i++) {
                if (Objects.equals(user, adminName[i]) && Objects.equals(password, adminPassword[i])) {
                    System.out.println("密码正确，登录成功");
                    z = 1;
                }
            }
            if (z == 0) {
                count++;
                if (count == 5) {//登录次数限制
                    System.out.println("尝试次数太多，程序已终止");
                    System.exit(1);//退出程序
                }
                System.out.println("登录失败，是否继续尝试？");
                System.out.println("|1.是\n|2.否（退出）");
                System.out.print("选择：>");
                int h = sc6.nextInt();
                if (h == 2)
                    main(names);
                else if (h == 1)
                    System.out.println("再登录失败" + (5 - count) + "次程序将终止");
            }
        }


    }

    public static void Register() {
        String user = "", password = "";
        int st = 0;
        System.out.println("注册管理员还是用户？");
        System.out.println("1.普通用户\n2.管理员");
        System.out.print("选择：>");
        int choose = sc6.nextInt();
        if (choose == 1) {
            for (; st == 0; ) {//只检查是否重复
                System.out.println("普通用户注册");
                System.out.print("输入账号：>");
                user = sc6.nextLine();
                user = sc6.nextLine();
                st = CheckUser(user);

            }
            state = 0;
            for (; state == 0; ) {
                System.out.println("输入密码（密码必须为8位以上，且必须含有数字与大小写字母）");
                System.out.print("输入密码：>");
                password = sc6.nextLine();
                state = CheckPassword(password);
            }
            userName = Arrays.copyOf(userName, userName.length + 1);
            userPassword = Arrays.copyOf(userPassword, userPassword.length + 1);
            userName[userName.length - 1] = user;
            userPassword[userPassword.length - 1] = password;
        } else if (choose == 2) {
            for (; st == 0; ) {//只检查是否重复
                System.out.println("管理员注册");
                System.out.print("输入账号：>");
                user = sc6.nextLine();
                user = sc6.nextLine();
                st = CheckAdmin(user);
            }
            state = 0;
            for (; state == 0; ) {
                System.out.println("输入密码（密码必须为8位以上，且必须含有数字与大小写字母）");
                System.out.print("输入密码：>");
                password = sc6.nextLine();
                state = CheckPassword(password);
            }
            adminName = Arrays.copyOf(adminName, adminName.length + 1);
            adminPassword = Arrays.copyOf(adminPassword, adminPassword.length + 1);
            adminName[adminName.length - 1] = user;
            adminPassword[adminPassword.length - 1] = password;
        }

    }

    public static void RestPassword() {
        String user, password;
        System.out.println("————重置密码————");
        System.out.println("1.管理员用户\n2.普通用户");
        System.out.print("选择：>");
        int choose = sc6.nextInt();
        if (choose == 1) {
            System.out.println("输入你要更改密码的管理员账号");
            System.out.print("输入：>");
            user = sc6.nextLine();
            user = sc6.nextLine();
            for (int i = 0; i < adminName.length; i++) {
                if (Objects.equals(user, adminName[i])) {
                    System.out.println("已验证");
                    for (int z = 0; z == 0; ) {//密码锁
                        System.out.println("输入新密码（密码必须为8位以上，且必须含有数字与大小写字母）");
                        System.out.print("输入：>");
                        password = sc6.nextLine();
                        z = CheckPassword(password);
                        adminPassword[i] = password;
                    }
                    System.out.println("密码已更改，重新登录！");
                    break;
                }
            }
        } else if (choose == 2) {
            System.out.println("输入你要更改密码的用户账号");
            System.out.print("输入：>");
            user = sc6.nextLine();
            user = sc6.nextLine();
            for (int i = 0; i < userName.length; i++) {
                if (Objects.equals(user, userName[i])) {
                    System.out.println("已验证");
                    for (int z = 0; z == 0; ) {
                        System.out.println("输入新密码（密码必须为8位以上，且必须含有数字与大小写字母）");
                        System.out.print("输入：>");
                        password = sc6.nextLine();
                        z = CheckPassword(password);
                        userName[i] = password;
                        System.out.println("密码已更改，重新登录！");
                    }
                    break;
                }
            }


        }

    }

    public static int CheckUser(String user) {
        for (int i = 0; i < userName.length; i++) {
            if (Objects.equals(user, userName[i])) {
                System.out.println("账号重复,请重新输入");
                return 0;
            }
        }
        return 1;

    }

    public static int CheckAdmin(String user) {
        for (int i = 0; i < adminName.length; i++) {
            if (Objects.equals(user, adminName[i])) {
                System.out.println("账号重复,请重新输入");
                return 0;
            }
        }
        return 1;

    }

    public static int CheckPassword(String password) {
        int s = 0, a = 0, b = 0, c = 0;
        if (password.length() < 8) {
            System.out.println("密码长度不够，请重新输入");
            return 0;
        }
        for (int i = 0; i < password.length(); i++) {
            char z = password.charAt(i);
            if (z >= '0' && z <= '9') {//筛选器判断密码是否只含有数字，大小写字母
                a = 1;
                continue;
            } else if (z >= 'a' && z <= 'z') {
                b = 1;
                continue;
            } else if (z >= 'A' && z <= 'Z') {
                c = 1;
                continue;
            }
            System.out.println("密码只能由数字与大小写字母组成，请重新输入");
            return 0;//密码格式不对
        }
        if (a + b + c == 3) { ////一共2^3种状态  1 1 1
            System.out.println("密码格式正确！");
            return 1;//通过
        } else {
            if (a == 0 && b == 0)
                System.out.println("密码缺少数字和小写字母，请重新输入");//0 0 1
            else if (a == 0 && c == 0)
                System.out.println("密码缺少数字和大写字母，请重新输入");//0 1 0
            else if (a == 0)
                System.out.println("密码缺少数字，请重新输入");//0 1 1
            else if (b == 0 && c == 1)
                System.out.println("密码缺少小写字母，请重新输入");//1 0 1
            else if (c == 0 && b == 1)
                System.out.println("密码缺少大写字母，请重新输入");// 1 1 0
            else if (c == 0) {// 1 0 0
                System.out.println("密码缺少大写和小写字母，请重新输入");
            }
            return 0;//0 0 0 格式不对
        }

    }

}

